package emulator.unittest.hardware.nmos6502.commands;


import static org.junit.Assert.assertEquals;

import org.junit.Test;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.commands.CPX;
import emulator.unittest.hardware.nmos6502.MockOperand;

public class CPXTest {

	@Test
	public void testExecute() throws EmulatorException {
		checkCMP(0, 0);
		checkCMP(0, 0x3E);
		checkCMP(0x3F, 0x3E);
		checkCMP(0x3F, 0x3F);
		checkCMP(0x3F, 0x40);
		checkCMP(0x3F, 0xFF);
		checkCMP(0xFF, 0);
	}

	private void checkCMP(int number, int operand) throws EmulatorException {
		Cpu6502 cpu = new Cpu6502();
		cpu.setX(new HwByte(number));
		CPX cmp = new CPX();
		cmp.execute(cpu, new MockOperand(operand));
		assertEquals(number, cpu.getX().getNumber());
		assertEquals(number == operand, cpu.getFlags().getZero());
		assertEquals(number >= operand, cpu.getFlags().getCarry());
		assertEquals(((number - operand) & 0x80) != 0, cpu.getFlags().getNegative());
	}

}
